package drds.server.config.model;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import drds.server.datasource.GroupDataSource;

/**
 * GroupDataSource是一组数据相同,权重不同的数据源
 */
public class GroupDataSourceConfig {
	public static final int NOT_SWITCH_DATASOURCE = -1;
	public static final int DEFAULT_SWITCH_DS = 1;
	public static final int SYN_STATUS_SWITCH_DS = 2;
	public static final int CLUSTER_STATUS_SWITCH_DS = 3;
	private static final Pattern pattern = Pattern.compile("\\s*show\\s+slave\\s+status\\s*", Pattern.CASE_INSENSITIVE);
	private static final Pattern patternCluster = Pattern.compile("\\s*show\\s+status\\s+like\\s+'wsrep%'", Pattern.CASE_INSENSITIVE);
	private String name;
	private int maxSize = DRDS.DEFAULT_POOL_SIZE;
	private int minSize = 10;
	/** 默认是写主库 */
	private int balanceType = GroupDataSource.BALANCE_TYPE_ON_WRITE;
	/** 默认是写主库 */

	private final MysqlInstanceInfo[] writeHosts;
	private final Map<Integer, MysqlInstanceInfo[]> readHostsMap;

	private String hearbeatSQL;
	private boolean isShowSlaveSql = false;
	private boolean isShowClusterSql = false;
	private int slaveThreshold = -1;
	private final int switchType;
	private String filters = "mergeStat";
	private long logTime = 300000;
	private boolean tempReadHostAvailable = false; // 如果写服务挂掉, 临时读服务是否继续可用

	public GroupDataSourceConfig(String name, MysqlInstanceInfo[] writeHosts, Map<Integer, MysqlInstanceInfo[]> readHostsMap, int switchType, int slaveThreshold, boolean tempReadHostAvailable) {
		super();
		this.name = name;

		this.writeHosts = writeHosts;
		this.readHostsMap = readHostsMap;
		this.switchType = switchType;
		this.slaveThreshold = slaveThreshold;
		this.tempReadHostAvailable = tempReadHostAvailable;
	}

	public boolean isTempReadHostAvailable() {
		return this.tempReadHostAvailable;
	}

	public int getSlaveThreshold() {
		return slaveThreshold;
	}

	public void setSlaveThreshold(int slaveThreshold) {
		this.slaveThreshold = slaveThreshold;
	}

	public int getSwitchType() {
		return switchType;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isShowSlaveSql() {
		return isShowSlaveSql;
	}

	public int getMaxSize() {
		return maxSize;
	}

	public void setMaxSize(int maxSize) {
		this.maxSize = maxSize;
	}

	public int getMinSize() {
		return minSize;
	}

	public void setMinCon(int minSize) {
		this.minSize = minSize;
	}

	public int getBalanceType() {
		return balanceType;
	}

	public void setBalanceType(int balanceType) {
		this.balanceType = balanceType;
	}

	public MysqlInstanceInfo[] getWriteHosts() {
		return writeHosts;
	}

	public Map<Integer, MysqlInstanceInfo[]> getReadHostsMap() {
		return readHostsMap;
	}

	public String getHearbeatSQL() {
		return hearbeatSQL;
	}

	public void setHearbeatSQL(String heartbeatSQL) {
		this.hearbeatSQL = heartbeatSQL;
		Matcher matcher = pattern.matcher(heartbeatSQL);
		if (matcher.find()) {
			isShowSlaveSql = true;
		}
		Matcher matcher2 = patternCluster.matcher(heartbeatSQL);
		if (matcher2.find()) {
			isShowClusterSql = true;
		}
	}

	public String getFilters() {
		return filters;
	}

	public void setFilters(String filters) {
		this.filters = filters;
	}

	public long getLogTime() {
		return logTime;
	}

	public boolean isShowClusterSql() {
		return this.isShowClusterSql;
	}

	public void setLogTime(long logTime) {
		this.logTime = logTime;
	}
}